﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using GestionPedago.Persistence.OPManager;
using Persistence.OPManager;

namespace GestionPedago.EntityContext {

    public partial class PlanningECF : EntityObject , IEquatable<PlanningECF> {

       /* public ECF GetECF()
        {
            return this.ECF;
        }

        public Promotion GetPromotion()
        {
            return this.Promotion;
        }*/

       /* public List<Stagiaire> GetStagiairesInscrits() {

            List<Stagiaire> lst = new List<Stagiaire>();

            foreach(PlanningECFDetail ped in this.PlanningECFDetail) {

                lst.Add(ped.PlanningIndividuelFormation.Stagiaire);

            }

            return lst;

        }*/

       /* public List<PlanningECFDetail> GetListePlanningECFDetail()
        {
            return (List < PlanningECFDetail >)this.PlanningECFDetail;

        }*/

      /*  public bool VerifierPertinencePlanification()
        {
            try
            {
                bool resultat = true;
                foreach (Module m in this.ECF.Module)
                {
                    if (resultat)
                    {
                        resultat = false;
                        foreach (Cours c in this.Promotion.Cours)
                        {
                            if (c.Module.IdModule == m.IdModule && c.Debut <= this.DateECF)
                            {
                                resultat = true;
                            }
                        }
                    }
                }
                return resultat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }*/

       /* public void AppliquerATousLesStagiairesPromotion(){
           
            List<PlanningECFDetail> lstDejaModifies = new List<PlanningECFDetail>();
            if (this.CodePromotion != null)
            {
                Promotion p = PromotionOPManager.GetPromotionByCode(this.CodePromotion);
                List<PlanningIndividuelFormation> listePif = PlanningIndividuelFormationOPManager.GetListePlanningIndividuelFormationByPromotion(p.CodePromotion);
                foreach (PlanningIndividuelFormation pif in listePif)
                {
                    bool ecfAPlanifier = true;
                    List<PlanningECFDetail> listePed = pif.PlanningECFDetail.ToList();
                    foreach (PlanningECFDetail ped in listePed)
                    {
                        if (ped.PlanningECF.IdECF == this.IdECF)
                        {
                            ecfAPlanifier = false;
                            if (ped.PlanningECF.CodePlanningECF == this.CodePlanningECF)
                            {
                                ped.PlanningECF = this;
                                ped.ContactENI = this.ContactENI;
                                ped.MiseAJourPourPlanificationParPromotion();
                                lstDejaModifies.Add(ped);
                            }
                        }
                    }
                    if (ecfAPlanifier)
                    {
                        PlanningECFDetail newPed = new PlanningECFDetail();
                        //EntityConnection.GetContext().PlanningECF.ApplyCurrentValues(this);
                        newPed.PlanningECF = this;
                        newPed.PlanningIndividuelFormation = pif;
                        newPed.ContactENI = this.ContactENI;
                        newPed.CodePlanning = pif.CodePlanning;
                       // EntityConnection.GetContext().PlanningECFDetail.Detach(newPed);
                        newPed = PlanningECFDetailOPManager.AjouterPlanningECFDetail(newPed, this.IdECF,true);
                        lstDejaModifies.Add(newPed);
                    }
                  

                }
            }

            List<PlanningECFDetail> listePed2 = PlanningECFDetailOPManager.GetListePlanningECFDetailByCodePlanningECF(this.CodePlanningECF);
            foreach (PlanningECFDetail ped in listePed2)
            {
                
                if (ped.PlanningECF.IdECF == this.IdECF)
                {
                    if (!lstDejaModifies.Contains(ped)) {
                        ped.PlanningECF = this;
                        ped.ContactENI = this.ContactENI;
                        ped.MiseAJourPourPlanificationParPromotion();
                        lstDejaModifies.Add(ped);
                    }
                }
            }
        }*/

        

        public PlanningECF MiseAJour()
        {
            try
            {
                //EntityConnection.GetContext().Detach(this.Promotion.Formation.Titre);
                EntityConnection.GetContext().SaveChanges();
                PlanningECFOPManager.AppliquerATousLesStagiairesPromotion(this);
                
                return this;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public bool Equals(PlanningECF other)
        {
            return this.CodePlanningECF == other.CodePlanningECF;
        }
    }

}
